package com.tourism.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tourism.common.Result;
import com.tourism.entity.Activity;
import com.tourism.mapper.ActivityMapper;
import com.tourism.service.ActivityService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity> implements ActivityService {


    @Override
    public Result getActivityList(Integer page, Integer pageSize, String activityName, String location) {
        Page<Activity> pageParam = new Page<>(page, pageSize);
        LambdaQueryWrapper<Activity> wrapper = new LambdaQueryWrapper<>();
        if (activityName != null && !activityName.isEmpty()) {
            wrapper.like(Activity::getActivityName, activityName);
        }
        if (location != null && !location.isEmpty()) {
            wrapper.like(Activity::getLocation, location);
        }
        // 检查 page 和 pageSize 是否为 null，如果是，则设置默认值
        if (page == null || pageSize == null) {
            pageParam = new Page<>(1, 10); // 默认第一页，每页 10 条数据
        }
        IPage<Activity> pageResult = page(pageParam, wrapper);
        return Result.success(pageResult);
    }



    @Override
    public Result getActivityById(Integer id) {
        if (id == null) {
            return Result.fail("活动ID不能为空");
        }
        Activity activity = getById(id);
        return activity != null ? Result.success(activity) : Result.fail("活动不存在");
    }

    @Override
    public Result addActivity(Activity activity) {
        if (activity.getActivityName() == null || activity.getActivityName().isEmpty()) {
            return Result.fail("活动名称不能为空");
        }
        if (activity.getStartTime() == null) {
            return Result.fail("开始时间不能为空");
        }
        if (activity.getEndTime() == null) {
            return Result.fail("结束时间不能为空");
        }

        boolean success = save(activity);
        return success ? Result.success("添加成功") : Result.fail("添加失败");
    }

    @Override
    public Result updateActivity(Activity activity) {
        if (activity.getId() == null) {
            return Result.fail("活动ID不能为空");
        }

        boolean success = updateById(activity);
        return success ? Result.success("更新成功") : Result.fail("更新失败");
    }

    @Override
    public Result deleteActivity(Integer id) {
        if (id == null) {
            return Result.fail("活动ID不能为空");
        }

        boolean success = removeById(id);
        return success ? Result.success("删除成功") : Result.fail("删除失败");
    }

    @Override
    @Transactional
    public Result batchDeleteActivities(List<Integer> ids) {
        if (ids == null || ids.isEmpty()) {
            return Result.fail("活动ID列表不能为空");
        }

        boolean success = removeBatchByIds(ids);
        return success ? Result.success("批量删除成功") : Result.fail("批量删除失败");
    }

    @Override
    public Result getAllActivities() {
        List<Activity> activities = list();
        return Result.success(activities);
    }
}
